<template>
   <view>
        <button class="btn" @click="clearToken">清除token</button>
        <button class="btn" @click="changeToken">更改token</button>
   </view>
</template>

<script>
export default {
    methods: {
        // 清除token
        clearToken() {
            try {
                const Token = uni.getStorageSync('Token')
                console.log(Token)
                if(Token) {
                     uni.removeStorage({
                        key: 'Token',
                        success: function (res) {
                            uni.showToast({
                                title: '清除成功'
                            })
                        }
                    });
                }else {
                    uni.showToast({
                        title: '请获取token',
                        icon: 'none'
                    })
                }
            }catch(e) {
                console.log(e) 
            }
        },
        // 更改token
        changeToken() {
            uni.getStorage({
                key: 'Token',
                success:function(res) {
                   if(res.data) {
                        uni.setStorage({
                            key: 'Token',
                            data: Math.round(Math.random() * 1000).toString(),
                            success:function() {
                                uni.showToast({
                                    title: '更改成功'
                                })
                            }
                        })
                   }        
                },
                fail: function(err) {
                    uni.showToast({
                        title: '请获取token',
                        icon: 'none'
                    })
                }
            })   
        }

    }
}
</script>

<style scoped>
    .btn {
        width: 80vw;
        margin: 40rpx auto;
    }
</style>